package com.AX.service.impl;


import com.AX.DTO.ParkingLotInfo;
import com.AX.DTO.ParkingSpaceStatus;
import com.AX.entity.ParkingLot;
import com.AX.mapper.ParkingLotMapper;
import com.AX.mapper.ParkingRecordMapper;
import com.AX.mapper.ParkingSpaceMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

//停车场信息服务
@Service
@RequiredArgsConstructor
public class ParkingLotService {
    private final ParkingLotMapper parkingLotMapper;
    private final ParkingSpaceMapper parkingSpaceMapper;

    private final ParkingRecordMapper parkingRecordMapper;

    // 功能一：获取所有停车场信息
    public List<ParkingLotInfo> getAllParkingLotsInfo() {
        return parkingLotMapper.findAll().stream()
                .map(lot -> new ParkingLotInfo(
                        lot.getParkingLotId(),
                        lot.getName(),
                        lot.getCoordinate(),
                        lot.getTotalSpaces(),
                        parkingLotMapper.countAvailableSpaces(lot.getParkingLotId())
                ))
                .collect(Collectors.toList());
    }

    // 功能二：获取指定停车场车位状态
    public List<ParkingSpaceStatus> getParkingSpaceStatus(Integer parkingLotId) {


        return parkingSpaceMapper.findByParkingLotId(parkingLotId).stream()
                .map(space -> new ParkingSpaceStatus(
                        space.getSpaceId(),
                        space.getStatus(),
                        space.getPosition()
                ))
                .collect(Collectors.toList());
    }

    //  功能三：获取指定停车场信息
    public ParkingLot getParkingLotStatus(Integer parkingLotId) {
        return parkingLotMapper.find(parkingLotId);
    }



    // 功能四： 修改车位状态
    public void updateSpaceStatus(Integer parkingLotId, String spaceId, String status) {
        parkingSpaceMapper.updateStatus(parkingLotId, spaceId, status);
    }

    //功能五：停车记录表计入
    public void insertParkingRecord(String spaceId,int userId)
    {
        String recordId = spaceId + String.valueOf(userId);
        parkingRecordMapper.insertRecord(recordId,spaceId,userId);
    }


    // 功能六：检查用户是否拥有车位记录
    public boolean checkSpaceOwnership(String spaceId, int userId) {
        return parkingRecordMapper.checkOwnership(userId, spaceId) > 0;
    }

    // 删除停车记录
    public void deleteParkingRecord(String spaceId, int userId) {
        parkingRecordMapper.deleteRecord(spaceId, userId);
    }
}
